package cn.blog.controller;

import cn.blog.entity.Blog;
import cn.blog.entity.PageBean;
import cn.blog.service.BlogService;
import cn.blog.util.ResponseUtil;
import cn.blog.util.StringUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 博客信息管理
 * 项目名：blog
 * 创建人：君忆zm
 * 创建时间：2019/10/22-11:43
 */
@Controller
@RequestMapping({"/admin/blog"})
public class BlogController {
    @Resource
    private BlogService blogService;

    /**
     * 保存一条博客
     */
    @RequestMapping({"/save"})
    public String save(Blog blog, HttpServletResponse response) throws Exception {
        Integer add = blogService.add(blog);
        JSONObject result = new JSONObject();
        if(add > 0){
            result.put("success",Boolean.valueOf(true));
        }else{
            result.put("success",Boolean.valueOf(false));
        }
        ResponseUtil.write(response,result);
        return null;
    }

    /**
     * 查询博客信息列表
     */
    @RequestMapping({"/list"})
    public String list(@RequestParam(value = "page",required = false)String page,
                       @RequestParam(value = "rows",required = false)String rows,
                       HttpServletResponse response,Blog blog) throws Exception {
        PageBean pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("start",pageBean.getPageSize());
        map.put("size",pageBean.getPageSize());
        map.put("title", StringUtil.formatLike(blog.getTitle()));
        //分页查询博客信息列表
        List<Blog> list = blogService.list(map);
        //获取共有多少条博客信息
        Long total = blogService.getTotal(map);
        //封装到JSON
        JSONObject result = new JSONObject();
        JSONArray jsonArray = JSONArray.fromObject(list);
        result.put("rows",jsonArray);
        result.put("total",total);
        ResponseUtil.write(response,result);
        return null;
    }

    /**
     * 根据主键查询一条博客信息
     */
    @RequestMapping({"findById"})
    public String findById(@RequestParam("id")String id,HttpServletResponse response) throws Exception {
        Blog blog = blogService.fingById(Integer.parseInt(id));
        JSONObject jsonObject = JSONObject.fromObject(blog);
        ResponseUtil.write(response,jsonObject);
        return null;
    }

    /**
     * 删除博客信息
     */
    @RequestMapping({"/delete"})
    public String delete(@RequestParam("id")String ids,HttpServletResponse response) throws Exception {
        String[] idsStr = ids.split(",");
        for(int i = 0;i<idsStr.length;i++){
            blogService.delete(Integer.parseInt(idsStr[i]));
        }
        JSONObject result = new JSONObject();
        result.put("success",Boolean.valueOf(true));
        ResponseUtil.write(response,result);
        return null;
    }
}
